package com.fhc.base

import android.app.Activity
import android.content.Context
import android.view.WindowManager
import androidx.compose.foundation.clickable
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.platform.LocalWindowInfo
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.core.view.WindowInsetsCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId


@Composable
fun KeepScreenOn() {
    val context = LocalContext.current
    DisposableEffect(Unit) {
        (context as? Activity)?.window?.addFlags(
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        )
        onDispose { /* 自动恢复 */ }
    }
}
@Composable
fun Float.pxToDp(): Dp {
    return with(LocalDensity.current) { this@pxToDp.toDp() }
}
@Composable
fun Int.pxToDp(): Dp {
    return with(LocalDensity.current) { this@pxToDp.toDp() }
}

fun String?.ifNoValue(block: () -> String): String {
    return if (this.isNullOrEmpty()) {
        block()
    } else {
        this
    }
}
fun String.noStyle(): AnnotatedString {
    return buildAnnotatedString {
        append(this@noStyle)
        // 可根据需求添加其他样式逻辑
    }
}
fun String.parseBold(): AnnotatedString {
    return buildAnnotatedString {
        append(this@parseBold)
        // 可根据需求添加其他样式逻辑
    }
}


fun String.epochMilli(): Long {
    return LocalDateTime.parse(this, YMDHMS)
        .atZone(ZoneId.systemDefault())
        .toInstant()
        .toEpochMilli()
}

fun Double.minMax(min: Double, max: Double): Double {
    if(this > max) return max
    if(this < min) return min
    return this
}
fun Int.minMax(min: Int, max: Int): Int {
    if(this > max) return max
    if(this < min) return min
    return this
}
fun Long.minMax(min: Long, max: Long): Long {
    if(this > max) return max
    if(this < min) return min
    return this
}



fun Long.ymdHMS(): String {
    return Instant.ofEpochMilli(this)
        .atZone(ZoneId.systemDefault())
        .format(YMDHMS)
}

fun Modifier.debouncedClickable(
    delayMillis: Long = 500L,
    onClick: () -> Unit
): Modifier = composed {
    var lastClickTime by remember { mutableLongStateOf(0L) }
    clickable {
        val currentTime = System.currentTimeMillis()
        if (currentTime - lastClickTime > delayMillis) {
            lastClickTime = currentTime
            onClick()
        }
    }
}

@Composable
fun Context.getNavigationBarHeight(): Dp {
    val window = (LocalView.current.context as Activity).window
    val insets = WindowInsetsCompat.toWindowInsetsCompat(
        window.decorView.rootWindowInsets,
        window.decorView
    ).getInsets(WindowInsetsCompat.Type.navigationBars())
    return with(LocalDensity.current) { insets.bottom.toDp() }
}


fun Context.isTablet(): Boolean {
    val metrics = resources.displayMetrics
    val widthDp = metrics.widthPixels / metrics.density
    return widthDp >= 600  // 安卓标准：≥600dp为平板‌:ml-citation{ref="2,5" data="citationList"}
}

@Composable
fun isTablet(): Boolean {
    val w = with(LocalDensity.current) {
        LocalWindowInfo.current.containerSize.width.toDp()
    }
    val h = with(LocalDensity.current) {
        LocalWindowInfo.current.containerSize.height.toDp()
    }
    return w >= 600.dp
}

fun showSnackbar(scope: CoroutineScope, snackbarHostState: SnackbarHostState, message: String, actionLabel: String? = null, onDismissed:()->Unit = {}, onActionPerformed:()->Unit= {}){
    scope.launch{
        val result = snackbarHostState.showSnackbar(
            message = message,
            actionLabel = actionLabel,
            withDismissAction = true,
            duration = SnackbarDuration.Long
        )
        when (result) {
            SnackbarResult.Dismissed -> onDismissed()
            SnackbarResult.ActionPerformed -> onActionPerformed()
        }
    }
}
